package com.silence.gankio.core.app

import java.util.HashMap

@Suppress("UNCHECKED_CAST")
class Configurator {
    private val LATTE_CONFIGS = HashMap<String, Any>()

    init {
        LATTE_CONFIGS[ConfigType.CONFIG_READY.name] = false
    }

    private object Holder {
        val INSTANCE = Configurator()
    }

    companion object {
        fun getInstance(): Configurator {
            return Holder.INSTANCE
        }
    }

    fun configure() {
        LATTE_CONFIGS[ConfigType.CONFIG_READY.name] = true
    }

    internal fun getCoreConfigs(): HashMap<String, Any> {
        return LATTE_CONFIGS
    }

    fun withApiHost(host: String): Configurator {
        LATTE_CONFIGS[ConfigType.API_HOST.name] = host
        return this
    }

    private fun checkConfiguration() {
        val isReady = LATTE_CONFIGS[ConfigType.CONFIG_READY.name] as Boolean
        if (!isReady) {
            throw RuntimeException("Configuration is not ready, call configure")
        }
    }

    internal fun <T> getConfiguration(key: Enum<ConfigType>): T {
        checkConfiguration()
        return LATTE_CONFIGS[key.name] as T
    }
}